/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//function to get the data by setting url, filter, success function and error function
var getUserPostion = function(url, phone, successFn, errorFn) {
	// making the ajax call
	$.ajax({
		url : url+'?phone='+phone,
		type : "GET",
		data : phone,
		success : function(resp) {
			// calling the user defined success function
			if (successFn)
				successFn(resp);
		},
		error : function(e) {
			// calling the user defined error function
			if (errorFn)
				errorFn(e);
		}
	});
}


var initData = function(phone) {
	var successFn = function(resp) {
		var data = '';
		if (resp) {
			// getting the data from the response object
			data = resp.data;
		}
		// creating the html content
		
		if (data.length > 0) {
			// init phone of user login
			var myLatlng = new google.maps.LatLng(data[0].latitude,
					data[0].longitude);
			var mapOptions = {
				zoom : 8,
				center : new google.maps.LatLng(data[0].latitude,
						data[0].longitude)
			};
			var image = {
				url : '/photo?action=display&id=' + data[0].avatar + '&userId='
						+ data[0].userId,
				size : new google.maps.Size(71, 71),
				origin : new google.maps.Point(0, 0),
				anchor : new google.maps.Point(17, 34),
				scaledSize : new google.maps.Size(50, 50)
			};
			map = new google.maps.Map(document.getElementById('map-canvas'),
					mapOptions);
			var marker = new MarkerWithLabel({
				position : myLatlng,
				map : map,
				icon : image,
				title : data[0].userId,
				labelContent: data[0].lastname + ' ' + data[0].firstname,
		        labelAnchor: new google.maps.Point(22, 0),
		        labelClass: "labels", // the CSS class for the label
		        labelStyle: {opacity: 0.75}
			});
			// load the other phones
			for ( var i = 1; i < data.length; i++) {
				myLatlng = new google.maps.LatLng(data[i].latitude,
						data[i].longitude);
				image = {
					url : '/photo?action=display&id=' + data[i].avatar
							+ '&userId=' + data[i].userId,
					size : new google.maps.Size(71, 71),
					origin : new google.maps.Point(0, 0),
					anchor : new google.maps.Point(17, 34),
					scaledSize : new google.maps.Size(50, 50)
				};
				marker = new MarkerWithLabel({
					position : myLatlng,
					map : map,
					icon : image,
					title : data[i].userId,
					labelContent: data[i].lastname + ' ' + data[i].firstname,
			        labelAnchor: new google.maps.Point(22, 0),
			        labelClass: "labels", // the CSS class for the label
			        labelStyle: {opacity: 0.75}
				});
			}
		} else {
			var mapOptions = {
				zoom : 8,
				center : new google.maps.LatLng(-34.397, 150.644)
			};
			map = new google.maps.Map(document.getElementById('map-canvas'),
					mapOptions);
		}
		
		var iw = new google.maps.InfoWindow({
		    content: "Home For Sale"
		  });
		
		google.maps.event.addListener(marker, "click", function (e) { iw.open(map, marker); });
	}
	getUserPostion("/index", phone, successFn, null);
}
